package com.sunmouse.toolkit.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.bean.ClassifyBean;
import com.sunmouse.toollib.adapter.BasicRecyclerAdapter;
import com.sunmouse.toollib.utils.StringUtils;

import java.util.List;

/**
 * @Author Kvin
 * @Email: 1746861438@qq.com
 * @data 2018/8/2 14:49
 */
public class ClassifyAdapter extends BasicRecyclerAdapter<ClassifyBean, ClassifyAdapter.ViewHolder> {

    private Context context;
    private OnSubItemClickListener mOnSubItemClickListener;

    public void setOnSubItemClickListener(OnSubItemClickListener listener) {
        this.mOnSubItemClickListener = listener;
    }

    public ClassifyAdapter(List<ClassifyBean> list, Context context) {
        super(list, context);
        this.context = context;
    }

    @Override
    protected View bindConvertView(LayoutInflater inflater, ViewGroup parent, int viewType) {
        return inflater.inflate(R.layout.adapter_classify, parent, false);
    }

    @Override
    protected ViewHolder bindHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    protected void bindData(List<ClassifyBean> list, final ViewHolder vh, int position) {
        ClassifyBean b = list.get(position);
        vh.titleTv.setText(StringUtils.getString(b.getName()));

        FunAdapter mAdapter = new FunAdapter(b.getSubData(), context);
        vh.mList.setLayoutManager(new GridLayoutManager(context, 4));
        vh.mList.setAdapter(mAdapter);
        vh.mList.setTag(position);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (mOnSubItemClickListener!=null)mOnSubItemClickListener.onSubItemClick((Integer) vh.mList.getTag(),position);
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleTv;
        RecyclerView mList;

        public ViewHolder(View v) {
            super(v);
            this.titleTv = (TextView) v.findViewById(R.id.title_tv);
            this.mList = (RecyclerView) v.findViewById(R.id.m_list);
        }
    }


    public interface OnSubItemClickListener {
        void onSubItemClick(int parentPosition, int childPosition);
    }
}
